package com.jxau.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxau.controller.utils.Result;
import com.jxau.domain.Cinema;
import com.jxau.domain.Comment;
import com.jxau.service.CinemaService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/MoviesWebsite/cinema")
public class CinemaController {
    @Resource
    private CinemaService cinemaService;

    @GetMapping
    public Result getAll(){
        return new Result(true, cinemaService.list());
    }

    @PostMapping
    public Result save(@RequestBody Cinema cinema){
        return new Result(cinemaService.save(cinema));
    }

    @PutMapping
    public Result modify(@RequestBody Cinema cinema){
        return new Result(cinemaService.updateById(cinema));
    }

    @DeleteMapping("{cinemaid}")
    public Result delete(@PathVariable Integer cinemaid){
        return new Result(cinemaService.removeById(cinemaid));
    }

    @GetMapping("{cinemaid}")
    public Result getById(@PathVariable Integer cinemaid){
        Cinema cinema =cinemaService.getById(cinemaid);
        if(cinema != null){
            return new Result(true, cinema);
        }else{
            return new Result(false, null);
        }
    }

    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, Cinema cinema){
        IPage<Cinema> page = cinemaService.getPage(currentPage, pageSize, cinema);
        if(currentPage > page.getPages()){
            page = cinemaService.getPage((int)page.getPages(), pageSize, cinema);
        }
        return new Result(true, page);
    }

}
